package com.jdym.back.controller;


import com.jdym.R;
import com.jdym.back.entity.Pledge;
import com.jdym.back.entity.Uploadfile;
import com.jdym.back.service.PledgeService;
import com.jdym.back.service.UploadfileService;
import com.jdym.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 上传资料 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@CrossOrigin
@RequestMapping("/back/uploadfile")
public class UploadfileController {
    @Autowired
    UploadfileService uploadfileService;
    @Autowired
    PledgeService pledgeService;
    SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd");

List<String> list=new ArrayList<>();

    @RequestMapping("/image")
    @ResponseBody
    public R image(MultipartFile file){
        //调用工具类完成文件上传
        System.out.println("file:"+file);
        String imagePath = UploadUtils.upload(file);
        System.out.println(imagePath);
        String url="http://localhost:8010"+imagePath;
        if(imagePath!=null){
            if(list.size()>6){
               list.clear();
            }else {
                list.add(url);
            }
            return R.ok().data("url",url);
        }else {
            return R.ok().data("url","上传失败");
        }
    }

    @RequestMapping("/saveFiles/{LoanId}")
    public R saveFiles(@RequestParam String pawn,@PathVariable int LoanId){
        System.out.println("list:"+list);
        Uploadfile uploadfile=new Uploadfile();
        uploadfile.setLoanid(LoanId);
        uploadfile.setPawn(pawn);
        if(list.size()<6){
            System.out.println(list.size());
            return R.error().data("msg","文件个数为"+list.size()+"小于6个,请重新上传");
        }else {
            if (list.get(0) != null) {
                uploadfile.setUpfileUrl((String) list.get(0));
            }
            if (list.get(1) != null) {
                uploadfile.setIdentity1((String) list.get(1));
            }
            if (list.get(2) != null) {
                uploadfile.setIdentity2((String) list.get(2));
            }
            if (list.get(3) != null) {
                uploadfile.setCertificate((String) list.get(3));
            }
            if (list.get(4) != null) {
                uploadfile.setCurrentAccount((String) list.get(4));
            }
            if (list.get(5) != null) {
                uploadfile.setCreditReport((String) list.get(5));
            }
            uploadfileService.getBaseMapper().insert(uploadfile);
            return R.ok().data("msg", "上传成功");
        }
    }



    //查询 pledge、firm、uploadfile三表 抵押登记信息
    @RequestMapping("/FindPleFirmUpload")
    public R FindPleFirmUpload() {
        List<Uploadfile> uploadfiles = uploadfileService.FindPleFirmUpload();
        for(int i=0;i<uploadfiles.size();i++){
            if(uploadfiles.get(i).getPledgeId()!=0){
               Pledge pledge=pledgeService.getBaseMapper().selectById(uploadfiles.get(i).getPledgeId());
                uploadfiles.get(i).setVoucher(pledge.getVoucher());
            }
        }
        return R.ok().data("uploadfiles", uploadfiles);
    }

    //    模糊查询 抵押登记手续 信息检索 通过企业名称
    @RequestMapping("/searchBuKeyWord")
    public R searchBuKeyWord(@RequestParam("keyWord") String keyWord) {
        System.out.println("ke:" + keyWord);
        List<Uploadfile> uploadfiles = uploadfileService.searchBuKeyWord(keyWord);
        return R.ok().data("uploadfiles", uploadfiles);
    }

    //上传
//    List<String> list = new ArrayList<>();

    @RequestMapping("/imagePledge")
    @ResponseBody
    public R imagePledge(MultipartFile file) {
        //调用工具类完成文件上传
        System.out.println("file:" + file);
        String imagePath = UploadUtils.upload(file);
        System.out.println(imagePath);
        String url = "http://localhost:8010" + imagePath;

        return R.ok().data("url", url);
    }

    @RequestMapping("/saveFilesPledge/{upfileid}")
    public R saveFiles(@PathVariable int upfileid,@RequestParam("url") String imgUrl) {//上传
        System.out.println("list:" + upfileid+":===="+imgUrl);
        Pledge pledge=new Pledge();
        pledge.setVoucher(imgUrl);
        System.out.println("插入的数据："+pledge);
        pledgeService.inserImgUrl(pledge);
        int pledgeId=pledge.getPledgeId();
        System.out.println(pledgeId);
//        更新
        Uploadfile uploadfile=new Uploadfile();

        uploadfile.setPledgeId(pledgeId);
        uploadfile.setUpfileid(upfileid);
        uploadfileService.getBaseMapper().updateById(uploadfile);
        return R.ok().data("msg", "上传成功");
    }

}

